package com.goodfox.web.biz.forest.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.goodfox.web.biz.forest.entity.CountyStatisticsResult;
import com.goodfox.web.biz.forest.entity.ForestStatisticsResult;
import com.goodfox.web.biz.forest.entity.TownshipStatisticsResult;
import com.goodfox.web.biz.forest.entity.VillageStatisticsResult;
import com.goodfox.web.biz.forest.service.StatisticsService;
import com.goodfox.web.generic.account.entity.Account;
import com.goodfox.web.generic.authority.service.PowerService;
import com.goodfox.web.generic.provide.service.EntityService;
import com.goodfox.web.generic.provide.service.ExcelTemplateService;
import com.goodfox.web.generic.provide.util.AttributeUtil;
import com.goodfox.web.generic.provide.util.DateUtil;

/**
 * 林权证Controller
 * @author sunyu
 *
 */
@Controller
@RequestMapping("/statistics")
public class StatisticsController {

	/** log4j日志器 */
	private Logger logger=Logger.getLogger(StatisticsController.class);
	
	/** 权限Service */
	@Autowired
	private PowerService powerService;
	
	/** 实体Service */
	@Autowired
	private EntityService entityService;
	
	/**统计分析Service */
	@Autowired
	private StatisticsService statisticsService;
	
	@Autowired
	private ExcelTemplateService excelTemplateService;


	/**
	 * 进入林权证管理首页
	 * @return 目标页面
	 */
	@RequestMapping(value="goStatisticsIndex.shtml")
	public String goStatisticsIndex(HttpServletRequest request,ModelMap model){
		logger.info("进入林权证统计首页");
		//判断是否有权限
	    Account loginAccount=powerService.havePower(request);
	    if(loginAccount!=null){
			//查询条件
		    Map queryMap=entityService.fillQueryMap(request,model);
		    if(queryMap.get("affDate")!=null && !String.valueOf(queryMap.get("affDate")).equals("")){
			    queryMap.put("affDate",DateUtil.dateFormat(String.valueOf(queryMap.get("affDate")),"yyyy"));
		    }
			logger.debug("--------------queryMap.get(areaId)------------------"+queryMap.get("areaId"));
		    if(queryMap.get("areaId")!=null && !String.valueOf(queryMap.get("areaId")).equals("")){
		    	queryMap.put("areaId", "'"+String.valueOf(queryMap.get("areaId")).replace(",", "','")+"'");
		    }
			//初始化页面各种下拉
			model.put("rstTypeList", AttributeUtil.queryAttrListByType("登记类型"));
			model.put("fstOwnList", AttributeUtil.queryAttrListByType("林地所有权权利人"));
			model.put("speciesList", AttributeUtil.queryAttrListByType("主要树种"));
			model.put("landTypeList", AttributeUtil.queryAttrListByType("地类"));
			model.put("fstTypeList", AttributeUtil.queryAttrListByType("森林分类"));
			model.put("fstSpeciesList", AttributeUtil.queryAttrListByType("林种"));
			
			//根据查询条件,进行统计分析
			List<ForestStatisticsResult> forestStatisticsResultList=statisticsService.forestStatistics(queryMap);
			//将统计分析结果传回前台页面
			model.put("forestStatisticsResultList", forestStatisticsResultList);
			logger.info("statistics/statisticsIndex");
			return "statistics/statisticsIndex";
	    }else{
	    	logger.info("redirect:/login/logout.shtml");
	    	return "redirect:/login/logout.shtml";
	    }
	}

	
	/**
	 * 执行林权证导出
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping(value="exportStatisticsAll.shtml")
	public void exportRightsAll(HttpServletRequest request,HttpServletResponse response,ModelMap model){
		logger.debug("执行林权证导出");
	    Account loginAccount=powerService.havePower(request);
	    if(loginAccount!=null){
		    Map queryMap=entityService.fillQueryMap(request,model);
		    if(queryMap.get("affDate")!=null && !String.valueOf(queryMap.get("affDate")).equals("")){
			    queryMap.put("affDate",DateUtil.dateFormat(String.valueOf(queryMap.get("affDate")),"yyyy"));
		    }
		    if(queryMap.get("areaId")!=null && !String.valueOf(queryMap.get("areaId")).equals("")){
		    	queryMap.put("areaId", "'"+String.valueOf(queryMap.get("areaId")).replace(",", "','")+"'");
		    }
		    List<CountyStatisticsResult> entityList=statisticsService.exportForestStatisticsList(queryMap);
		    excelTemplateService.webExpWorkbook(request, response, CountyStatisticsResult.class, entityList);
	    }
	}
	
}
